package com.heima.wemedia.listener;

import com.heima.common.constants.wemedia.WemediaNewsAutoScanConstants;
import com.heima.wemedia.service.IWmNewsAutoScanService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j
public class WmNewsAutoScanListener {

    @Autowired
    private IWmNewsAutoScanService wmNewsAutoScanService;

    @RabbitListener(queuesToDeclare = @Queue(name = WemediaNewsAutoScanConstants.WM_NEWS_AUTO_SCAN_QUEUE))
    public void listenerAutoScanMessage(@Payload String newsId, Message message, Channel channel){
        log.info("成功接收到文章自动审核消息, 消息队列: {}, 文章 id: {}", WemediaNewsAutoScanConstants.WM_NEWS_AUTO_SCAN_QUEUE, newsId);
        /*消费者消息确认 - 手动确认*/
        try {
            wmNewsAutoScanService.autoScanWmNews(Integer.parseInt(newsId));
            /*获取消息的唯一标识 ID*/
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("消费者处理消息时出现异常，异常原因: {}", e.getMessage());
        }
    }

}
